package com.ruoyi.sxt.service.impl;

import java.util.ArrayList;
import java.util.List;

import com.ruoyi.common.core.domain.Ztree;
import com.ruoyi.common.core.domain.entity.SxtJuese;
import com.ruoyi.common.core.domain.entity.SysMenu;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.sxt.domain.SxtCaidan;
import com.ruoyi.sxt.mapper.SxtCaidanMapper;
import com.ruoyi.sxt.service.ISxtCaidanService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;



import com.ruoyi.common.core.text.Convert;

/**
 * 菜单权限Service业务层处理
 * 
 * @author ruoyi
 * @date 2025-07-02
 */
@Service
public class SxtCaidanServiceImpl implements ISxtCaidanService
{
    @Autowired
    private SxtCaidanMapper sxtCaidanMapper;

    /**
     * 查询菜单权限
     * 
     * @param menuId 菜单权限主键
     * @return 菜单权限
     */
    @Override
    public SxtCaidan selectSxtCaidanByMenuId(Long menuId)
    {
        return sxtCaidanMapper.selectSxtCaidanByMenuId(menuId);
    }

    /**
     * 查询菜单权限列表
     * 
     * @param sxtCaidan 菜单权限
     * @return 菜单权限
     */
    @Override
    public List<SxtCaidan> selectSxtCaidanList(SxtCaidan sxtCaidan)
    {
        return sxtCaidanMapper.selectSxtCaidanList(sxtCaidan);
    }

    /**
     * 新增菜单权限
     * 
     * @param sxtCaidan 菜单权限
     * @return 结果
     */
    @Override
    public int insertSxtCaidan(SxtCaidan sxtCaidan)
    {
        sxtCaidan.setCreateTime(DateUtils.getNowDate());
        return sxtCaidanMapper.insertSxtCaidan(sxtCaidan);
    }

    /**
     * 修改菜单权限
     * 
     * @param sxtCaidan 菜单权限
     * @return 结果
     */
    @Override
    public int updateSxtCaidan(SxtCaidan sxtCaidan)
    {
        sxtCaidan.setUpdateTime(DateUtils.getNowDate());
        return sxtCaidanMapper.updateSxtCaidan(sxtCaidan);
    }

    /**
     * 批量删除菜单权限
     * 
     * @param menuIds 需要删除的菜单权限主键
     * @return 结果
     */
    @Override
    public int deleteSxtCaidanByMenuIds(String menuIds)
    {
        return sxtCaidanMapper.deleteSxtCaidanByMenuIds(Convert.toStrArray(menuIds));
    }

    @Override
    public List<Ztree> menuTreeData()
    {
        List<SxtCaidan> menuList = sxtCaidanMapper.selectSxtCaidanList(new SxtCaidan());
        List<Ztree> ztrees = initZtree(menuList);
        return ztrees;
    }

    public List<Ztree> menuTreeData(SxtJuese sxtJuese)
    {
        List<Ztree> ztrees = new ArrayList<Ztree>();
        List<SxtCaidan> menuList = sxtCaidanMapper.selectSxtCaidanList(new SxtCaidan());
        if (StringUtils.isNotNull(sxtJuese.getRoleId()))
        {
            List<String> roleMenuList = sxtCaidanMapper.selectSxtCaidanByJueseId(sxtJuese);
            ztrees = initZtree(menuList, roleMenuList, true);
        }
        else
        {
            ztrees = initZtree(menuList, null, true);
        }
        //List<Ztree> ztrees = initZtree(menuList);
        return ztrees;
    }

    public List<Ztree> initZtree(List<SxtCaidan> menuList, List<String> roleMenuList, boolean permsFlag)
    {
        List<Ztree> ztrees = new ArrayList<Ztree>();
        boolean isCheck = StringUtils.isNotNull(roleMenuList);
        for (SxtCaidan menu : menuList)
        {
            Ztree ztree = new Ztree();
            ztree.setId(menu.getMenuId());
            ztree.setpId(menu.getParentId());
            ztree.setName(transMenuName(menu, permsFlag));
            ztree.setTitle(menu.getMenuName());
            if (isCheck)
            {
                ztree.setChecked(roleMenuList.contains(menu.getMenuId() + menu.getPerms()));
            }
            ztrees.add(ztree);
        }
        return ztrees;
    }

    public List<Ztree> initZtree(List<SxtCaidan> menuList)
    {
        return initZtree(menuList, null, false);
    }

    public String transMenuName(SxtCaidan menu, boolean permsFlag)
    {
        StringBuffer sb = new StringBuffer();
        sb.append(menu.getMenuName());
        if (permsFlag)
        {
            sb.append("<font color=\"#888\">&nbsp;&nbsp;&nbsp;" + menu.getPerms() + "</font>");
        }
        return sb.toString();
    }

    /**
     * 删除菜单权限信息
     * 
     * @param menuId 菜单权限主键
     * @return 结果
     */
    @Override
    public int deleteSxtCaidanByMenuId(Long menuId)
    {
        return sxtCaidanMapper.deleteSxtCaidanByMenuId(menuId);
    }
}
